{// 引入标签库 }
<tagLib name="html" />
<include file="Public:header" />
<php>function get_status_text($v){return $v==1?'进行中':'已开奖';}</php>
<div style="height:10px;"></div>
<div style="margin:0 10px">
    <a href="#" onclick="location.reload()">刷新</a><br />
    <fieldset>
        <legend>推流地址</legend>
        <table class="dataTable">
            <tr><th>推流地址：</th><td id="push_rtmp">{$video.push_rtmp}</td></tr>
            <tr><th>关闭直播：</th><td>
                    <a href="/mapi/index.php?ctl=video&act=end_video&user_id={$xiacai.user_id}&room_id={$xiacai.room_id}" class="btn-close">[关闭]</a>
            <if condition="empty($video['push_rtmp'])">
                <a href="/m.php?m=Xiacai&a=error_reset&xiacai_id={$xiacai.id}&room_id={$xiacai.room_id}" title="网络掉线等导致的直播房间出错,无法用正常的关闭方法来终止网综节目,可以用这个按钮来清理异常数据">[异常重置]</a>
            </if>
            <if condition="$video['push_rtmp']">
                <span>心跳监控：<span id="span-monitor-cd" data-url="/mapi/index.php?ctl=video&act=monitor&user_id={$xiacai.user_id}&room_id={$xiacai.room_id}" title="{$video['live_in']}">0</span></span>
            </if>
            </td>
            </tr>
        </table>
    </fieldset>
</div>
<div style="margin:0 10px; width:390px; float:left;">
    <fieldset>
        <legend>节目信息</legend>
        <table class="dataTable">
            <tr><th>节目名称：</th><td title="节目编号：{$xiacai.id}">{$xiacai.subject}</td></tr>
            <tr><th>直播房间号：</th><td>{$xiacai.room_id}</td></tr>
            <if condition="count($guessinglist) gt 0">
                <tr><th>竞猜状态：</th><td><span id="span-guessing-status">{$guessing_status}</span></td></tr>
                <tr><th>操作：</th><td>
                <if condition="$guessing_status eq '进行中'">
                    <select name="guessing_id">
                        <option value="">==选择竞猜项目==</option>
                        <foreach name="guessinglist" item="vo">
                            <option value="{$vo.id}">{$vo.title}</option>
                        </foreach>
                    </select>
                    <button type="button" id="btn-exec" data-value="draw">竞猜开奖</button>
                    <div style="color:#F00; margin-top:3px;" id="errmsg"></div>
                    <else/>
                    <button type="button" id="btn-exec" data-value="start">竞猜开始</button>
                </if>
                <form id="frm-opt" action="/mapi/index.php?ctl=guessing&act=varact">
                    <input type="hidden" name="xiacai_id" value="{$xiacai.id}" />
                    <input type="hidden" name="video_id" value="{$xiacai.room_id}" />
                </form>
                </td></tr>
            </if>
        </table>
    </fieldset>
    <if condition="count($guessinglist) gt 0">
        <fieldset>
            <legend>竞猜项目</legend>
            <table class="dataTable">
                <tr><th>名称</th><th>倍率</th></tr>
                <foreach name="guessinglist" item="vo">
                    <tr><td title="竞猜编号：{$vo.id}">{$vo.title}</td><td>{$vo.win_ratio}</td></tr>
                </foreach>
            </table>
        </fieldset>
        <fieldset>
            <legend>开奖历史</legend>
            <div style="max-height: 270px; overflow-y: auto">
                <table class="dataTable">
                    <tr><th>轮数</th><th>开奖结果</th><th>开奖时间</th><th>状态</th></tr>
                    <php>$guessingloglist_count=count($guessingloglist);</php>
                    <foreach name="guessingloglist" item="vo">
                        <tr><td>{$guessingloglist_count-$key}</td><td title="开奖编号：{$vo.id}">{$vo.win_guessing_title}</td><td>{$vo.draw_time|to_date}</td><td>{$vo.status|get_status_text}</td></tr>
                    </foreach>
                </table>
            </div>
        </fieldset>
    </if>
</div>
<script>
    var push_rtmp=$('#push_rtmp').text();
    if(push_rtmp){
        var apush_rtmp=push_rtmp.split('/live/');
        $('#push_rtmp').html(apush_rtmp[0]+'/live/<hr />'+apush_rtmp[1]);
    }
    if ($('#span-monitor-cd')[0]) {
        setInterval(monitor, 1000); // 30秒请求一次心跳包
    }
    // 直播心跳 2017-11-8
    function monitor(url) {
        var $span = $('#span-monitor-cd');
        var count = $span.text() * 1;
        console.log(count);
        if (count > 0) {
            count += 1;
            if (count > 30) {
                count = 0;
            }
            $span.text(count);
            return;
        }
        console.log('get');
        $.get($span.data('url'), function (result) {
            if (typeof (result) == 'string') {
                try {
                    result = JSON.parse(result);
                } catch (e) {
                    alert(e);
                    return;
                }
                if (result.status) {
                    $span.text(1);
                } else {
                    alert(result.error);
                }
            }
        });
    }
    // 关闭直播
    $('.btn-close').click(function () {
        if (!confirm("确定关闭这个直播？")) {
            return false;
        }
        var url = $(this).attr('href');
        $.post(url, function (result) {
            result = JSON.parse(result);
            if (result.status) {
                location.reload();
            } else {
                alert(result.error);
            }
        });
        return false;
    })
    // 操作处理 2017-10-18
    $('#btn-exec').click(function () {
        var opt = $(this).data('value');
        if (!opt) {
            return false;
        }
        var tip = '';
        var guessing_id = 0;
        switch (opt) {
            case 'start':
                tip = '你确定要开始一轮新的竞猜吗?';
                break;
            case 'draw':
                guessing_id = $('select[name="guessing_id"]').val();
                if (!guessing_id) {
                    $('#errmsg').text('*请选择一个获胜的竞猜项目!');
                    return false;
                }
                tip = '您确定要将 [' + $('select[name="guessing_id"] option[value="' + guessing_id + '"]').text() + '] 作为本轮竞猜的获胜选项吗?';
                break;
        }
        if (!confirm(tip)) {
            return false;
        }
        var $frm = $('#frm-opt');
        var postdata = $frm.serialize();
        if (opt == 'draw') {
            postdata += '&guessing_id=' + guessing_id;
        }
        console.log(postdata);
//        /mapi/index.php?ctl=guessing&act=start
        $.get($frm.attr('action').replace('varact', opt), postdata, function (result) {
            console.log(result);
            if (typeof (result) == 'string') {
                try {
                    result = $.parseJSON(result);
                } catch (e) {
                    alert(e);
                    return false;
                }
            }
            console.log(result);
            if (result.success) {
                location.reload();
            } else {
                alert(result.message);
            }
        })
        return false;
    })
</script>
<input type="hidden" id="live_in" value="{$video.live_in}"/>
<div style="float:left"><div id="video-container" style="margin: 0px auto"></div></div>
<div style="clear:both"></div>

<input type="hidden" id="video_type" value="{$video.video_type}"/>
<input type="hidden" id="rtmp_url" value="{$video.rtmp_url}"/>
<input type="hidden" id="flv_url" value="{$video.flv_url}"/>
<input type="hidden" id="flv_hd_url" value="{$video.flv_hd_url}"/>
<input type="hidden" id="flv_sd_url" value="{$video.flv_sd_url}"/>
<input type="hidden" id="m3u8_url" value="{$video.m3u8_url}"/>
<input type="hidden" id="m3u8_hd_url" value="{$video.m3u8_hd_url}"/>
<input type="hidden" id="m3u8_sd_url" value="{$video.m3u8_sd_url}"/>
<input type="hidden" id="mp4_url" value="{$video.mp4_url}"/>
<input type="hidden" id="mp4_hd_url" value="{$video.mp4_hd_url}"/>
<input type="hidden" id="mp4_sd_url" value="{$video.mp4_sd_url}"/>
<script src="http://imgcache.qq.com/open/qcloud/video/vcplayer/TcPlayer.js"></script>
<script>
    var live = $("#live_in").val();
    var video_type = $("#video_type").val();
    var rtmp_url = $("#rtmp_url").val();
    var flv_url = $("#flv_url").val();
    var flv_hd_url = $("#flv_hd_url").val();
    var flv_sd_url = $("#flv_sd_url").val();
    var m3u8_url = $("#m3u8_url").val();
    var m3u8_hd_url = $("#m3u8_hd_url").val();
    var m3u8_sd_url = $("#m3u8_sd_url").val();
    var mp4_url = $("#mp4_url").val();
    var mp4_hd_url = $("#mp4_hd_url").val();
    var mp4_sd_url = $("#mp4_sd_url").val();

    (function () {
        function getParams(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURIComponent(r[2]);
            }
            return null;
        }
        var rtmp = getParams('rtmp'),
                flv = getParams('flv'),
                m3u8 = getParams('m3u8'),
                mp4 = getParams('mp4'),
                live = (getParams('live') == 'true' ? true : false),
                coverpic = getParams('coverpic'),
                width = getParams('width'),
                height = getParams('height'),
                autoplay = (getParams('autoplay') == 'true' ? true : false);
        /**
         * 视频类型播放优先级
         * mobile ：m3u8>mp4
         * PC ：RTMP>flv>m3u8>mp4
         */
        var options = {
            rtmp: rtmp,
            flv: flv,
            flv_hd: flv,
            flv_sd: flv,
            m3u8: m3u8 || m3u8_url,
            m3u8_hd: m3u8 || m3u8_hd_url,
            m3u8_sd: m3u8 || m3u8_sd_url,
            mp4: mp4 || mp4_url,
            mp4_hd: mp4_hd_url,
            mp4_sd: mp4_sd_url,
            coverpic: coverpic,
            autoplay: autoplay ? true : false,
            live: live,
            width: width || '800',
            height: height || '600'
        };
        var player = new TcPlayer('video-container', options);
        window.qcplayer = player;
    })();
</script>